package com.baizhi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayInputStream;
import java.io.IOException;

@RestController
public class CodeController {
    @RequestMapping("code")
    public void Code(HttpSession session, HttpServletResponse response) throws IOException {
        //产生验证码图片
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116,36,4,20);
        //获取response的响应流
        ServletOutputStream outputStream = response.getOutputStream();
        //把图片放入responsed的相应流中
        lineCaptcha.write(outputStream);
        //获取到生成的验证码值
        String code = lineCaptcha.getCode();
        //放入session中
        session.setAttribute("code",code);
        //关闭流
        outputStream.close();
    }
}
